package airarmory.item
{
	import airarmory.define.ItemConst;
	import airarmory.orm.vo.TblItemVO;
	
	public class AbstractItemParser
	{
		protected var lines:Array;
		protected var itemVo:TblItemVO;
		private var validateResult:String;
		
		public function AbstractItemParser(itemString:String):void
		{
		}

		protected function getItemId(itemName:String):String
		{
			return ItemConst.getItemID(itemName);
		}
		protected function getIsToken(item_level:int):Boolean
		{
			return item_level < 200;
		}
		protected function getItemLevel():int
		{
			for each(var line:String in this.lines)
			{
				if(line.indexOf("Item Level") != -1)
				{
					var a:Array = line.split("Item Level ");
					return int(a[a.length - 1]);
				}
			}
			return -1;
		}
		protected function getSlot():String
		{
			for each(var line:String in this.lines)
			{
				if(ItemConst.SLOT_OBEJCT[line] != null)
					return line;
			}
			return "";
		}
		protected function getType():String
		{
			for each(var line:String in this.lines)
			{
				if(ItemConst.TYPE_OBEJCT[line] != null)
					return line;
			}
			return "";
		}
		protected function getIsUnique():Boolean
		{
			for each(var line:String in this.lines)
			{
				if(line == "Unique")
					return true;
			}
			return false;
		}
		protected function fitProperty():void
		{
			for each(var line:String in this.lines)
			{
				if(ItemConst.hasTankProperty(line))
					this.itemVo.fit_tank = true;
				else if(ItemConst.hasCasterProperty(line))
					this.itemVo.fit_caster = true;
				else if(ItemConst.hasMeleeProperty(line))
					this.itemVo.fit_melee = true;
			}
		}
		protected function fitClassByAssigned():void
		{
			for each(var line:String in this.lines)
			{
				if(line.indexOf("Classes: ") != -1)
				{
					var classes:Array = line.substr("Classes: ".length).split(", ");
					for each(var classs:String in classes)
					{
						classs = classs.replace(" ", "");
						this.itemVo["fit_" + classs.toLowerCase()] = true;
					}
				}
			}
		}
		protected function fitClassByType(type:String, slot:String):void
		{
			if(!this.isFitClassSuccess())
				this.fitClassByArmor(type);
				
			if(!this.isFitClassSuccess())
				this.fitClassByWeapon(type);
				
			if(!this.isFitClassSuccess())
				this.fitClassByOffHand(type);
				
			if(!this.isFitClassSuccess())
				this.fitClassByRanged(type);
				
			if(!this.isFitClassSuccess() && ItemConst.isAllClassesSlot(slot))
				this.fitAllClasses(type);
		}
		protected function fitClassByArmor(type:String):void
		{
			if(type == "Plate")
			{
				this.itemVo.fit_deathknight = true;
				this.itemVo.fit_paladin = true;
				this.itemVo.fit_warrior = true;
			}
			else if(type == "Mail")
			{
				this.itemVo.fit_shaman = true;
				this.itemVo.fit_hunter = true;
			}
			else if(type == "Leather")
			{
				this.itemVo.fit_druid = true;
				this.itemVo.fit_rogue = true;
			}
			else if(type == "Cloth")
			{
				this.itemVo.fit_priest = true;
				this.itemVo.fit_warlock = true;
				this.itemVo.fit_mage = true;
			}
		}
		protected function fitClassByWeapon(type:String):void
		{
			// Death Knight: Axes, Polearms, Swords, Two-Handed Axes, Two-Handed Swords, One-handed Maces, Two-Handed Maces
			// Druid: Staves, Polearms, Maces (with training), Daggers (with training), Unarmed Weapons (with training)
			// Hunter: Axes (Dwarves/Tauren start, others with training), Daggers (Night Elves and Blood Elves start, others with training), Guns or Bows, Crossbows (Draenei start, others with training), Fist Weapons (with training), Polearms (with training), Staves (with training), Swords (Draenei start, others with training), Thrown (with training), Two-Handed Axes (with training), Two-Handed Swords (with training)
			// Mage: Staves, Wands, Daggers (with training), Swords (with training)
			// Paladin: One- and Two-handed Maces, One- and Two-handed Swords (with training), One- and Two-handed Axes, Polearms (with training)
			// Priest: One-Handed Maces, Daggers, Staves, Wands
			// Rogue: Daggers, Thrown, Swords (with training), Bows (with training), Crossbows (with training), Maces (with training), Fist Weapons (with training), Guns (with training)
			// Shaman: One-Handed Maces, Staves, Fist Weapons (Draenei with training), One-Handed Axes (with training), Daggers (with training), Two-Handed Axes (with training), Two-Handed Maces (with training)
			// Warlock: Daggers, Wands, Swords (with training), Staves (with training)
			// Warrior: All (excluding wands)
			if(this.itemVo.slot == "Main Hand" || this.itemVo.slot == "Off Hand" || this.itemVo.slot == "One-Hand")
			{
				if(type == "Axe")
				{
					this.itemVo.fit_deathknight = true;
					this.itemVo.fit_hunter = true;
					this.itemVo.fit_paladin = true;
					this.itemVo.fit_shaman = true;
					this.itemVo.fit_warrior = true;
				}
				else if(type == "Mace")
				{
					this.itemVo.fit_deathknight = true;
					this.itemVo.fit_druid = true;
					this.itemVo.fit_paladin = true;
					this.itemVo.fit_priest = true;
					this.itemVo.fit_rogue = true;
					this.itemVo.fit_shaman = true;
					this.itemVo.fit_warrior = true;
				}
				else if(type == "Sword")
				{
					this.itemVo.fit_deathknight = true;
					this.itemVo.fit_hunter = true;
					this.itemVo.fit_paladin = true;
					this.itemVo.fit_mage = true;
					this.itemVo.fit_warlock = true;
					this.itemVo.fit_rogue = true;
					this.itemVo.fit_warrior = true;
				}
				else if(type == "Fist Weapon")
				{
					this.itemVo.fit_hunter = true;
					this.itemVo.fit_rogue = true;
					this.itemVo.fit_shaman = true;
					this.itemVo.fit_warrior = true;
					this.itemVo.fit_druid = true;
				}
				else if(type == "Dagger")
				{
					this.itemVo.fit_warrior = true;
					this.itemVo.fit_shaman = true;
					this.itemVo.fit_hunter = true;
					this.itemVo.fit_druid = true;
					this.itemVo.fit_rogue = true;
					this.itemVo.fit_priest = true;
					this.itemVo.fit_warlock = true;
					this.itemVo.fit_mage = true;
				}
			}
			else if(this.itemVo.slot == "Two-Hand")
			{
				if(type == "Axe")
				{
					this.itemVo.fit_deathknight = true;
					this.itemVo.fit_hunter = true;
					this.itemVo.fit_paladin = true;
					this.itemVo.fit_shaman = true;
				}
				else if(type == "Mace")
				{
					this.itemVo.fit_deathknight = true;
					this.itemVo.fit_druid = true;
					this.itemVo.fit_paladin = true;
					this.itemVo.fit_shaman = true;
				}
				else if(type == "Sword")
				{
					this.itemVo.fit_deathknight = true;
					this.itemVo.fit_hunter = true;
					this.itemVo.fit_paladin = true;
					this.itemVo.fit_warrior = true;
				}
				else if(type == "Polearm")
				{
					this.itemVo.fit_deathknight = true;
					this.itemVo.fit_paladin = true;
					this.itemVo.fit_warrior = true;
					this.itemVo.fit_hunter = true;
					this.itemVo.fit_druid = true;
				}
				else if(type == "Staff")
				{
					this.itemVo.fit_hunter = true;
					this.itemVo.fit_druid = true;
					this.itemVo.fit_mage = true;
					this.itemVo.fit_priest = true;
					this.itemVo.fit_shaman = true;
					this.itemVo.fit_warlock = true;
				}
			}
		}
		protected function fitClassByOffHand(type:String):void
		{
			if(type == "Shield")
			{
				this.itemVo.fit_paladin = true;
				this.itemVo.fit_warrior = true;
				this.itemVo.fit_shaman = true;
			}
			else if(type == "Held In Off-Hand")
			{
				this.itemVo.fit_priest = true;
				this.itemVo.fit_warlock = true;
				this.itemVo.fit_mage = true;
			}
		}
		protected function fitClassByRanged(type:String):void
		{
			if(type == "Gun"
				|| type == "Bow"
				|| type == "Thrown"
				|| type == "Crossbow")
			{
				this.itemVo.fit_hunter = true;
				this.itemVo.fit_rogue = true;
				this.itemVo.fit_warrior = true;
			}
			else if(type == "Wand")
			{
				this.itemVo.fit_priest = true;
				this.itemVo.fit_warlock = true;
				this.itemVo.fit_mage = true;
			}
			else if(type == "Idol")
			{
				this.itemVo.fit_druid = true;
			}
			else if(type == "Libram")
			{
				this.itemVo.fit_paladin = true;
			}
			else if(type == "Totem")
			{
				this.itemVo.fit_shaman = true;
			}
			else if(type == "Sigil")
			{
				this.itemVo.fit_deathknight = true;
			}
		}
		protected function fitAllClasses(type:String):void
		{
			this.itemVo.fit_deathknight = true;
			this.itemVo.fit_paladin = true;
			this.itemVo.fit_warrior = true;
			this.itemVo.fit_shaman = true;
			this.itemVo.fit_hunter = true;
			this.itemVo.fit_druid = true;
			this.itemVo.fit_rogue = true;
			this.itemVo.fit_priest = true;
			this.itemVo.fit_warlock = true;
			this.itemVo.fit_mage = true;
		}
		
		protected function isFitClassSuccess():Boolean
		{
			return this.itemVo.fit_deathknight
				|| this.itemVo.fit_paladin
				|| this.itemVo.fit_warrior
				|| this.itemVo.fit_shaman
				|| this.itemVo.fit_hunter
				|| this.itemVo.fit_druid
				|| this.itemVo.fit_rogue
				|| this.itemVo.fit_priest
				|| this.itemVo.fit_warlock
				|| this.itemVo.fit_mage;
		}
		protected function validate():void
		{
			this.validateResult = "";
			
			if(this.itemVo.item_id == "" || this.itemVo.item_id.length > 5)
				dealValidateFailed("item_id");
			if(!this.itemVo.is_token)
			{
				if(this.itemVo.slot == "")
					dealValidateFailed("slot");
						
				if(this.itemVo.type == "")
					dealValidateFailed("type");
				
				if(!(this.itemVo.fit_tank || this.itemVo.fit_caster || this.itemVo.fit_melee))
					dealValidateFailed("fit_property");
			}
			
			if(!isFitClassSuccess())
				dealValidateFailed("fit_class");
		}
		private function dealValidateFailed(failed:String):void
		{
			validateResult = validateResult
				+ this.itemVo.item_id + "\t" + this.itemVo.item_name + "\t-->\t" + failed + " failed\n";
		}
		public function getParseResult():TblItemVO
		{
			return this.itemVo;
		}
		public function getValidateResult():String
		{
			return this.validateResult
		}
	}
}